package com.hz.ws;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.hz.utils.ExceptionCatchUtil;
import com.hz.utils.RandomUtil;
import com.hz.utils.SftpUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(tags = "上传管理", description = "operation about upload")
@RestController
@RequestMapping("/file")
public class UpOrDownloadController {
	@Value("${sftp.host}")
	private String sftpHost;
	@Value("${sftp.port}")
	private int sftpPort;
	@Value("${sftp.username}")
	private String sftpUsername;
	@Value("${sftp.password}")
	private String sftpPassword;
	@Value("${sftp.baseUrl}")
	private String sftpBaseUrl;
	private static SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd");

	/**
	 * 上传sftp
	 * 
	 * @param file
	 *            文件
	 * @return result:result[0] fileName,result[1] 上传后文件名,result[2]
	 *         fileDirectory,result[3] uploadState,result[4] errorCode
	 */
	@ApiOperation(value = "上传sftp", notes = "根据文件上传sftp")
	@RequestMapping(value = "/uploadSftp", method = RequestMethod.POST)
	@ApiImplicitParams(@ApiImplicitParam(name = "file", value = "文件流对象", required = true, dataType = "MultipartFile", allowMultiple = true))
	public String[] uploadSftp(MultipartFile file) {
		String directory = "/upload" + sdf.format(new Date());
		String[] result = new String[5];
		SftpUtil sftp = new SftpUtil(sftpUsername, sftpPassword, sftpHost, sftpPort);
		sftp.login();
		InputStream is = null;
		result[0] = file.getOriginalFilename();
		result[1] = RandomUtil.getUUID();
		result[2] = directory;
		try {
			is = file.getInputStream();
			sftp.upload(sftpBaseUrl, directory, result[1], is);
			result[3] = "success";
			sftp.logout();
			is.close();
		} catch (Exception e) {
			result[3] = "false";
			result[4] = ExceptionCatchUtil.ExceptionCatch(e).getResultCode();
			sftp.logout();
			e.printStackTrace();
			try {
				if (is != null)
					is.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			sftp.logout();
			try {
				if (is != null)
					is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	/**
	 * 下载sftp
	 * 
	 * @param directory
	 * @param fileName
	 * @param savePath
	 * @return result:result[0] fileName,result[1] fileDirectory,result[2]
	 *         uploadState,result[3] errorCode
	 */
	@ApiOperation(value = "下载sftp", notes = "从sftp上下载文件")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "directory", value = "文件所在sftp目录", required = true, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "fileName", value = "文件所在sftp中的文件名称", required = true, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "savePath", value = "下载本地路径\\文件原名", required = true, dataType = "String", paramType = "query") })
	@RequestMapping(value = "/downloadSftp", method = { RequestMethod.POST, RequestMethod.GET })
	public String[] downloadSftp(String directory, String fileName, String savePath) {
		String[] result = new String[4];
		SftpUtil sftp = new SftpUtil(sftpUsername, sftpPassword, sftpHost, sftpPort);
		sftp.login();
		result[0] = fileName;
		result[1] = savePath;
		try {
			sftp.download(directory, fileName, savePath);
			result[2] = "success";
			sftp.logout();
		} catch (Exception e) {
			result[2] = "false";
			result[3] = ExceptionCatchUtil.ExceptionCatch(e).getResultCode();
			sftp.logout();
		} finally {
			sftp.logout();
		}
		return result;
	}

	/**
	 * 删除sftp
	 * 
	 * @param directory
	 * @param fileName
	 * @return result:result[0] deleteState,result[1] errorCode
	 */
	@ApiOperation(value = "删除sftp", notes = "从sftp上删除文件")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "directory", value = "文件所在sftp目录", required = true, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "fileName", value = "文件所在sftp中的文件名称", required = true, dataType = "String", paramType = "query") })
	@RequestMapping(value = "/deleteSftp", method = { RequestMethod.POST, RequestMethod.GET })
	public String[] deleteSftp(String directory, String fileName) {
		String[] result = new String[2];
		SftpUtil sftp = new SftpUtil(sftpUsername, sftpPassword, sftpHost, sftpPort);
		sftp.login();
		try {
			sftp.delete(directory, fileName);
			result[0] = "success";
			sftp.logout();
		} catch (Exception e) {
			result[0] = "false";
			result[1] = ExceptionCatchUtil.ExceptionCatch(e).getResultCode();
			sftp.logout();
		} finally {
			sftp.logout();
		}
		return result;
	}

	public String getSftpHost() {
		return sftpHost;
	}

	public void setSftpHost(String sftpHost) {
		this.sftpHost = sftpHost;
	}

	public int getSftpPort() {
		return sftpPort;
	}

	public void setSftpPort(int sftpPort) {
		this.sftpPort = sftpPort;
	}

	public String getSftpUsername() {
		return sftpUsername;
	}

	public void setSftpUsername(String sftpUsername) {
		this.sftpUsername = sftpUsername;
	}

	public String getSftpPassword() {
		return sftpPassword;
	}

	public void setSftpPassword(String sftpPassword) {
		this.sftpPassword = sftpPassword;
	}

	public String getSftpBaseUrl() {
		return sftpBaseUrl;
	}

	public void setSftpBaseUrl(String sftpBaseUrl) {
		this.sftpBaseUrl = sftpBaseUrl;
	}
}
